package com.useeinfo.oa.modules.repair.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.func.lang.StringUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.enums.ResultEnums;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.DictInfoBiz;
import com.useeinfo.oa.modules.base.biz.ResourceBiz;
import com.useeinfo.oa.modules.base.entity.DictInfo;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Resource;
import com.useeinfo.oa.modules.repair.dao.RepairWorkLogDao;
import com.useeinfo.oa.modules.repair.entity.RepairOrder;
import com.useeinfo.oa.modules.repair.entity.RepairWorkLog;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Auther: xudong.sun
 * @Date: 2018/12/25 11:17
 * @Description:
 */
@Service("repairWorkLogBiz")
public class RepairWorkLogBiz extends CrudBiz<RepairWorkLogDao, RepairWorkLog> {

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private ResourceBiz resourceBiz;

    @Autowired
    private DictInfoBiz dictInfoBiz;


    @Autowired
    private RepairOrderBiz repairOrderBiz;

    public ResultDto addLog(String logId, String workLogCatId, String workLogLevelId, String workContent, String photos,String repairOrderId) {
        RepairWorkLog repairWorkLog;
        if (StringUtils.isNoneBlank(logId)) {
            repairWorkLog = dao.findModel(StringConverters.null2Long(logId));
        } else {
            repairWorkLog = new RepairWorkLog();
        }

        Enterprise sessionEnterprise = SessionKeyUtils.getSessionEnterprise(httpServletRequest);
        SystemUser systemUser = SessionKeyUtils.getSessionUser(httpServletRequest);
        if (systemUser == null || sessionEnterprise == null) {
            ResultUtil.error(ResultEnums.NO_LOGIN_MESSAGE);
        }
        repairWorkLog.setCode(dao.getCode(StringConverters.null2String(sessionEnterprise.getId())));
        repairWorkLog.setWorkContent(StringConverters.null2String(workContent));

        if (StringUtils.isNotBlank(photos)) {
            JSONArray jsonArray = JSONArray.parseArray(photos);
            if (jsonArray.size() > 0) {
                Set<Resource> resources = new HashSet<>();
                for (Object id : jsonArray) {
                    Resource resource = resourceBiz.findModel(StringConverters.null2Long(id));
                    resources.add(resource);
                }
                repairWorkLog.setLogPhoto(resources);
            }
        }

        repairWorkLog.setSystemUser(systemUser);

        if (StringUtils.isNotBlank(workLogCatId)) {
            DictInfo workLogCat = dictInfoBiz.findModel(StringConverters.null2Long(workLogCatId));
            repairWorkLog.setWorkLogCat(workLogCat);
        }

        if (StringUtils.isNotBlank(workLogLevelId)) {
            DictInfo workLogLevel = dictInfoBiz.findModel(StringConverters.null2Long(workLogLevelId));
            repairWorkLog.setWorkLogLevel(workLogLevel);
        }

        if(StringUtils.isNotBlank(repairOrderId)){
            RepairOrder repairOrder = repairOrderBiz.findModel(StringConverters.null2Long(repairOrderId));
            repairWorkLog.setRepairOrder(repairOrder);
        }
        addOrUpdate(repairWorkLog);
        return ResultUtil.success();
    }

    public ResultDto repairWorkLogList(String pageIndex,String pageSize,String repairOrderId){
        QueryParam queryParam = new QueryParam(pageIndex,pageSize);
        if(StringUtils.isNotBlank(repairOrderId)){
            queryParam.getSqlMap().put("repairOrderId",repairOrderId);
        }
        return ResultUtil.success(findJSONList(queryParam));
    }

    public ResultDto repairWorkLogDetail(String repairWorkLogId){
        if(StringUtils.isBlank(repairWorkLogId)){
            return ResultUtil.error("参数错误");
        }
        RepairWorkLog repairWorkLog = findModel(StringConverters.null2Long(repairWorkLogId));
        return ResultUtil.success(repairWorkLog.toJSONObjectDetail());
    }
}
